package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.MessageCenter;
import com.innovations.inn.service.IMessageCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消息中心Controller
 *
 * @author dp
 * @date 2024-08-09
 */
@Api(tags = "消息中心")
@RestController
@RequestMapping("/inn/message/center")
public class MessageCenterController extends BaseController {
    @Autowired
    private IMessageCenterService messageCenterService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询消息中心列表
     */
    @ApiOperation("查询消息中心列表")
    @PreAuthorize("@ss.hasPermi('inn:center:list')")
    @GetMapping("/list")
    public TableDataInfo list(MessageCenter messageCenter) {
        startPage();
        List<MessageCenter> list = messageCenterService.selectMessageCenterList(messageCenter);
        return getDataTable(list);
    }

    /**
     * 导出消息中心列表
     */
    @PreAuthorize("@ss.hasPermi('inn:center:export')")
    @Log(title = "消息中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageCenter messageCenter) {
        List<MessageCenter> list = messageCenterService.selectMessageCenterList(messageCenter);
        ExcelUtil<MessageCenter> util = new ExcelUtil<MessageCenter>(MessageCenter.class);
        util.exportExcel(response, list, "消息中心数据");
    }

    /**
     * 获取消息中心详细信息
     */
    @ApiOperation("获取消息中心详细信息")
    @PreAuthorize("@ss.hasPermi('inn:center:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId) {
        return success(messageCenterService.selectMessageCenterByMessageId(messageId));
    }

    /**
     * 新增消息中心
     */
    @ApiOperation("新增消息中心")
    @PreAuthorize("@ss.hasPermi('inn:center:add')")
    @Log(title = "消息中心", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody MessageCenter messageCenter) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        messageCenterService.completeData(messageCenter, userId, 1);
        return toAjax(messageCenterService.insertMessageCenter(messageCenter));
    }

    /**
     * 修改消息中心
     */
    @ApiOperation("修改消息中心")
    @PreAuthorize("@ss.hasPermi('inn:center:edit')")
    @Log(title = "消息中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody MessageCenter messageCenter) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        messageCenterService.completeData(messageCenter, userId, 2);
        return toAjax(messageCenterService.updateMessageCenter(messageCenter));
    }

    /**
     * 删除消息中心
     */
    @ApiOperation("删除消息中心")
    @PreAuthorize("@ss.hasPermi('inn:center:remove')")
    @Log(title = "消息中心", businessType = BusinessType.DELETE)
    @DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds) {
        return toAjax(messageCenterService.deleteMessageCenterByMessageIds(messageIds));
    }
}
